package web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import vo.Log;
import vo.User;
import business.LogService;
import business.UserService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("unchecked")
public class LogAction implements Preparable {
	private LogService service;
	private UserService userService;
	private List<Log> logs;
	private ArrayList<String> delIds = new ArrayList<String>();

	// 分页变量
	private int curPage;
	private int totalPage;
	private int pageSize = 5;

	private User curUser;

	public String listLog() {
		if (curPage == 0)
			curPage = 1;
		System.out.println("curPage:" + curPage);
		int totalCount = (int) service.getTotalCount();
		System.out.println("totalCount:" + totalCount);
		if (totalCount % pageSize == 0)
			totalPage = totalCount / pageSize;
		else
			totalPage = totalCount / pageSize + 1;
		System.out.println("totalCount / pageSize:" + totalCount / pageSize);

		this.logs = service.getAllLogs((curPage - 1) * pageSize, pageSize);
		return Action.SUCCESS;
	}

	public String addLog() {
		service.addLog("hello xxx");
		return listLog();
	}

	public String deleteLog() {
		System.out.println("delete log:");
		for (int i = 0; i < this.delIds.size(); i++) {
			System.out.println("deleted qq ids：" + delIds.get(i));
			long id = Long.parseLong(delIds.get(i));
			service.delete(id);

		}
		return "logDeleted";
	}

	public List<Log> getLogs() {
		return logs;
	}

	public void prepare() throws Exception {
		ActionContext ctx = ActionContext.getContext();
		Map session = ctx.getSession();
		String username = (String) session.get("username");
		curUser = userService.getByLoginName(username);
	}

	public void setLogService(LogService service) {
		this.service = service;
	}

	public ArrayList<String> getDelIds() {
		return delIds;
	}

	public void setDelIds(ArrayList<String> delIds) {
		this.delIds = delIds;
	}

	public int getCurPage() {
		return curPage;
	}

	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public User getCurUser() {
		return curUser;
	}

	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}

	public void setLogs(List<Log> logs) {
		this.logs = logs;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public void setService(LogService service) {
		this.service = service;
	}

}